package com.cpi.cornpp.bushelprice.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;
import org.apache.struts.util.MessageResources;

import KC.ModSegApp.UsuarioApp;

import com.cpi.cornpp.bushelprice.exception.BushelPriceException;
import com.cpi.cornpp.bushelprice.form.BushelPriceForm;
import com.cpi.cornpp.bushelprice.services.BushelPriceService;
import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.user.view.UserView;
/**
 * Bushel Price Action
 * 
 * @author Edgar Joao
 *
 */
public class BushelPriceAction extends MappingDispatchAction{
	/** Default forward */
	private final static String DEFAULT_FORWARD = "default";//$NON-NLS-1$
	/** Custom exception */
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	/** Information message name*/
	private final static String MESSAGE = "MESSAGE";//$NON-NLS-1$
	/** Success forward*/
	private static final String SUCCESS_FORWARD = "success";//$NON-NLS-1$
	/** Success edit key */
	private static final String SUCCESS_EDIT_LABEL = "common.edit.success";//$NON-NLS-1$
	/** Success add key */
	private static final String SUCCESS_ADD_LABEL = "common.add.success";//$NON-NLS-1$
	/** Nombre de variable de session para el usuario */
	private static final String USER_SESSION_NAME = "user";//$NON-NLS-1$
	/**
	 * Accion que muestra la pantalla de Precio Bushel
	 * @author Edgar Joao
	 * @param argMapping
	 * @param argForm
	 * @param argRequest
	 * @param argResponse
	 * @return
	 * @throws Exception
	 */
	public ActionForward goToBushelPrice(ActionMapping argMapping, ActionForm argForm,
			HttpServletRequest argRequest, HttpServletResponse argResponse)
			throws Exception {
		
		BushelPriceService service = null;
		BushelPriceForm bushelPriceForm = null;
		List bushelPriceList = null;
		HttpSession httpSession = argRequest.getSession(true);
		int orderField = 1;
		int orderType = 0;
		
		try{
			bushelPriceForm = (BushelPriceForm) argForm;
			service = new BushelPriceService();
			bushelPriceList = new ArrayList(0);
			
			if(bushelPriceForm != null){
				if(bushelPriceForm.getOrderField() > 0){
					orderField = bushelPriceForm.getOrderField();
				}
				if(bushelPriceForm.getOrderType() > 0){
					orderType = bushelPriceForm.getOrderType();
				}							
			}						
			bushelPriceList = service.list(orderField, (1 == orderType) ? false : true, bushelPriceForm.getBushelPriceView());
			argRequest.setAttribute("bushelPriceList", bushelPriceList);
			httpSession.setAttribute("previousOrderField", Integer.toString(bushelPriceForm.getOrderField()));
			httpSession.setAttribute("previousOrderType", Integer.toString(bushelPriceForm.getOrderType()));			
			
		}catch (BushelPriceException bushelPriceException){
			httpSession.setAttribute(CUSTOM_EXCEPTION, bushelPriceException);
			throw bushelPriceException;
		}catch (Exception exception){
			BushelPriceException bushelPriceException = null;
			exception.printStackTrace(System.out);
			bushelPriceException= new BushelPriceException(exception, CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			httpSession.setAttribute(CUSTOM_EXCEPTION, bushelPriceException);
			throw bushelPriceException;
		}
		
		return argMapping.findForward(DEFAULT_FORWARD);
	}
	/**
	 * @author Edgar Joao
	 * @param argMapping
	 * @param argForm
	 * @param argRequest
	 * @param argResponse
	 * @return
	 * @throws Exception
	 */
	public ActionForward goToAddBushelPrice(ActionMapping argMapping, ActionForm argForm,
			HttpServletRequest argRequest, HttpServletResponse argResponse)
			throws Exception {
		
		return argMapping.findForward(DEFAULT_FORWARD);
	}
	/**
	 * Accion que guarda un precio bushel
	 * @author Edgar Joao
	 * @param argMapping
	 * @param argForm
	 * @param argRequest
	 * @param argResponse
	 * @return
	 * @throws Exception
	 */
	public ActionForward addBushelPrice(ActionMapping argMapping, ActionForm argForm,
			HttpServletRequest argRequest, HttpServletResponse argResponse)
			throws Exception {	
		BushelPriceForm bushelPriceForm = null;		
		BushelPriceService service      = null;
		MessageResources messageResources = null;
		String messageError = null;		
		HttpSession httpSession = argRequest.getSession(true);		
		UserView userView = null;		
		UsuarioApp usuarioApp;
		
		try{
			bushelPriceForm = (BushelPriceForm) argForm;
			messageResources = getResources(argRequest);
			service = new BushelPriceService();		
			//Se obtiene el usuario
			userView = (UserView) httpSession.getAttribute(USER_SESSION_NAME);
			usuarioApp =  (UsuarioApp) userView.getUserObject();
			bushelPriceForm.getBushelPriceView().setUserId(usuarioApp.getLogin());		
			service.insertBushelPrice(bushelPriceForm.getBushelPriceView());
			messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), SUCCESS_ADD_LABEL);
			httpSession.setAttribute(MESSAGE, messageError);
		}catch (BushelPriceException bushelPriceException){
			httpSession.setAttribute(CUSTOM_EXCEPTION, bushelPriceException);
			throw bushelPriceException;
		}catch (Exception exception){
			BushelPriceException bushelPriceException = null;
			exception.printStackTrace(System.out);
			bushelPriceException= new BushelPriceException(exception, CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			httpSession.setAttribute(CUSTOM_EXCEPTION, bushelPriceException);
			throw bushelPriceException;
		}		
		return argMapping.findForward(SUCCESS_FORWARD);
	}
	/**
	 * @author Edgar Joao
	 * @param argMapping
	 * @param argForm
	 * @param argRequest
	 * @param argResponse
	 * @return
	 * @throws Exception
	 */
	public ActionForward goToEditBushelPrice(ActionMapping argMapping, ActionForm argForm,
			HttpServletRequest argRequest, HttpServletResponse argResponse)
			throws Exception {

		BushelPriceForm bushelPriceForm = null;		
		BushelPriceService service      = null;
		String bushelId = null;
		HttpSession httpSession = argRequest.getSession(true);				
		try{
			bushelId = (String) argRequest.getParameter("bushelId");
			bushelPriceForm = (BushelPriceForm) argForm;
			service = new BushelPriceService();
			bushelPriceForm.setBushelPriceView(service.getBushelPriceView(Integer.parseInt( bushelId )));			
		}catch (BushelPriceException bushelPriceException){
			httpSession.setAttribute(CUSTOM_EXCEPTION, bushelPriceException);
			throw bushelPriceException;
		}catch (Exception exception){
			BushelPriceException bushelPriceException = null;
			exception.printStackTrace(System.out);
			bushelPriceException= new BushelPriceException(exception, CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			httpSession.setAttribute(CUSTOM_EXCEPTION, bushelPriceException);
			throw bushelPriceException;
		}						
		return argMapping.findForward(DEFAULT_FORWARD);
	}	
	/**
	 * Accion que edita un precio bushel
	 * @author Edgar Joao
	 * @param argMapping
	 * @param argForm
	 * @param argRequest
	 * @param argResponse
	 * @return
	 * @throws Exception
	 */
	public ActionForward editBushelPrice(ActionMapping argMapping, ActionForm argForm,
			HttpServletRequest argRequest, HttpServletResponse argResponse)
			throws Exception {
		BushelPriceForm bushelPriceForm = null;		
		BushelPriceService service      = null;
		MessageResources messageResources = null;
		String messageError = null;		
		HttpSession httpSession = argRequest.getSession(true);		
		UserView userView = null;		
		UsuarioApp usuarioApp;
		
		try{
			bushelPriceForm = (BushelPriceForm) argForm;
			messageResources = getResources(argRequest);
			service = new BushelPriceService();
			//Se obtiene el usuario
			userView = (UserView) httpSession.getAttribute(USER_SESSION_NAME);
			usuarioApp =  (UsuarioApp) userView.getUserObject();
			bushelPriceForm.getBushelPriceView().setUserId(usuarioApp.getLogin());			
			service.editBushelPrice(bushelPriceForm.getBushelPriceView());			
			messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), SUCCESS_EDIT_LABEL);
			httpSession.setAttribute(MESSAGE, messageError);
		}catch (BushelPriceException bushelPriceException){
			httpSession.setAttribute(CUSTOM_EXCEPTION, bushelPriceException);
			throw bushelPriceException;
		}catch (Exception exception){
			BushelPriceException bushelPriceException = null;
			exception.printStackTrace(System.out);
			bushelPriceException= new BushelPriceException(exception, CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			httpSession.setAttribute(CUSTOM_EXCEPTION, bushelPriceException);
			throw bushelPriceException;
		}
		return argMapping.findForward(SUCCESS_FORWARD);
	}
	/**
	 * Metodo que realiza la busqueda por fechas
	 * @author Edgar Joao
	 * 
	 * @param argMapping
	 * @param argForm
	 * @param argRequest
	 * @param argResponse
	 * @return
	 * @throws Exception
	 */
	public ActionForward bushelPriceFilterSearch(ActionMapping argMapping, ActionForm argForm,
			HttpServletRequest argRequest, HttpServletResponse argResponse)
			throws Exception {
		
		BushelPriceService service = null;
		BushelPriceForm bushelPriceForm = null;
		List bushelPriceList = null;
		HttpSession httpSession = argRequest.getSession(true);
		int orderField = 1;
		int orderType = 0;
		
		try{
			bushelPriceForm = (BushelPriceForm) argForm;
			service = new BushelPriceService();
			bushelPriceList = new ArrayList(0);
			
			if(bushelPriceForm != null){
				if(bushelPriceForm.getOrderField() > 0){
					orderField = bushelPriceForm.getOrderField();
				}
				if(bushelPriceForm.getOrderType() > 0){
					orderType = bushelPriceForm.getOrderType();
				}
			}			
			bushelPriceList = service.list(orderField, (1 == orderType) ? false : true, bushelPriceForm.getBushelPriceView());
			argRequest.setAttribute("bushelPriceList", bushelPriceList);
			httpSession.setAttribute("previousOrderField", Integer.toString(bushelPriceForm.getOrderField()));
			httpSession.setAttribute("previousOrderType", Integer.toString(bushelPriceForm.getOrderType()));			
		}catch (BushelPriceException bushelPriceException){
			httpSession.setAttribute(CUSTOM_EXCEPTION, bushelPriceException);
			throw bushelPriceException;
		}catch (Exception exception){
			BushelPriceException bushelPriceException = null;
			exception.printStackTrace(System.out);
			bushelPriceException= new BushelPriceException(exception, CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			httpSession.setAttribute(CUSTOM_EXCEPTION, bushelPriceException);
			throw bushelPriceException;
		}
		return argMapping.findForward(DEFAULT_FORWARD);
	}
}
